home *** CD-ROM | disk | FTP | other *** search
- Path: inforamp.net!ts1-10
- From: rmorin@inforamp.net (Randy Charles Morin)
- Newsgroups: comp.lang.c++
- Subject: Re: Help with converting string to float, please!!!
- Date: Sun, 03 Mar 96 07:21:51 GMT
- Organization: MiddleWorld SoftWare
- Message-ID: <4hbha6$899@sam.inforamp.net>
- References: <4h208l$aja@news1.sunbelt.net>
- NNTP-Posting-Host: ts1-10.tor.inforamp.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4h208l$aja@news1.sunbelt.net>, kberry@safefed.org (Kevin) wrote:
- >I need to parse up a string and perform calculations on the variables.
- >take this string char *Query="APR=8.9&TERM=5&AMOUNT=10000"
- >How would I parse it up and take out the numbers so I can use them in
- floating
- >point calculations?
- >Then I need to put them back in a string for output.
-
- This code is far from optimized. But the general feel is there.
-
- main()
- {
- yourString strAPR, strTERM, strAMOUNT;
- yourDouble fAPR, fTERM, fAMOUNT;
- is >> strAPR >> fAPR >> strTERM >> fTERM >> strAMOUNT >> fAMOUNT;
- os << strAPR << fAPR << '&'
- << strTERM << fTERM << '&'
- << strAMOUNT << fAMOUNT;
- };
-
- istream& operator >> (istream& s, yourString& strYours)
- {
- char c;
- while (1)
- {
- s >> c;
- addcharactertoyourString(c,strYours);
- if (c=='=') break;
- }
- }
-
- istream& operator >> (istream& s, yourDouble& fYours)
- {
- char strTemp[64];
- char c;
- while (1)
- {
- s >> c;
- if ((c=='&') || (c=='\0')) break;
- addcharactertoyourString(c,strTemp);
- }
- fYours = ftoa(strTemp);
- }
-
-